﻿using MediatR;
using Microsoft.EntityFrameworkCore;
using SmartCity.ProcessEngine.Api.Read.Application.Command.From;
using SmartCity.ProcessEngine.Api.Read.Dtos;
using SmartCity.ProcessEngine.Domain.Entities;
using SmartCity.ProcessEngine.ErrorCode;
using SmartCity.ProcessEngine.Infrastructure.Extention;
using SmartCity.ProcessEngine.Infrastructure.Interface;

namespace SmartCity.ProcessEngine.Api.Read.Application.CommandHandler.From
{
    public class GetFormHandler : IRequestHandler<GetFormCommand, ApiPaging<FormDto>>
    {
        private readonly IBaseRepository<FormModel> formRepo;
        private readonly IBaseRepository<FormCategoryModel> formCategoryRepo;
        private readonly IBaseRepository<IndustryModel> industryRepo;

        /// <summary>
        /// 注入查询所需要的仓储
        /// </summary>
        /// <param name="formRepo"></param>
        /// <param name="formCategoryRepo"></param>
        /// <param name="industryRepo"></param>
        public GetFormHandler(IBaseRepository<FormModel> formRepo, IBaseRepository<FormCategoryModel> formCategoryRepo, IBaseRepository<IndustryModel> industryRepo)
        {
            this.formRepo = formRepo;
            this.formCategoryRepo = formCategoryRepo;
            this.industryRepo = industryRepo;
        }
        public async Task<ApiPaging<FormDto>> Handle(GetFormCommand request, CancellationToken cancellationToken)
        {
            ApiPaging<FormDto> res = new ApiPaging<FormDto>();

            var forms = formRepo.GetAll().Where(x => !x.IsDeleted);
            var formCategorys = formCategoryRepo.GetAll().Where(x => !x.IsDeleted);
            var industrys = industryRepo.GetAll().Where(x => !x.IsDeleted);

            if (request.FormName.IsNotNull())
            {//如果流程表单名称不为空,则根据流程表单名称过滤表单
                forms = forms.Where(x => x.FormName.Contains(request.FormName));
            }

            //linq 外联查询,只有主表的条件在查询前判断,其他连表的条件都在查询表达式里判断
            var linq = from f in forms
                       join c in formCategorys on f.CategoryId equals c.Id into fc
                       from c in fc.DefaultIfEmpty()
                       join i in industrys on c.IndustryId equals i.Id into ci
                       from i in ci.DefaultIfEmpty()
                       select new FormDto
                       {
                           Id = f.Id,
                           FormName = f.FormName,
                           CategoryId = c.Id,
                           CategoryCode = c.FormCategoryCode,
                           CategoryName = c.FormCategoryName,
                           FormCode = f.FormCode,
                           IndustryName = i.IndustryName,
                           IndustryId = i.Id,
                           FormDes=f.FormDes
                       };

            //根据行业名称判断
            if (request.IndustryId!=null) {
                linq = linq.Where(x=>x.IndustryId==request.IndustryId);
            }
            //根据类别code判断
            if(request.CategoryCode.IsNotNull())
            {
                linq = linq.Where(x => x.CategoryCode.Contains(request.CategoryCode));
            }

            res.TotalCount = linq.Count();
            res.PageDatas = linq.OrderByDescending(x=>x.Id).Skip((request.PageIndex-1)*request.PageSize).Take(request.PageSize).ToList();
            res.Msg = "分页查询流程表单列表成功!";
            res.Code = ApiEnum.Success;
            return res;
        }
    }
}
